package cn.tedu.lmx.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class JdbcUtils {
    //类中有两个方法
    //1.1更新方法，支持insert,update,delete 作用：返回的是受影响的行数
    //public static int update(String sql)
    //1.2查询方法，支持select 作用：打印查询的结果
    //public static void select (String sql)
    public static int update(String sql) {

        String url="jdbc:mysql://localhost:3306/test?useSSL=false";
        String username = "root";
        String password = "root";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection connection = DriverManager.getConnection(url, username, password);
            Statement statement = connection.createStatement();
            return statement.executeUpdate(sql);

        }catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
    public static void select (String sql) {
        String url="jdbc:mysql://localhost:3306/test?useSSL=false";
        String username = "root";
        String password = "root";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection connection = DriverManager.getConnection(url, username, password);
            Statement statement = connection.createStatement();
            ResultSet set = statement.executeQuery(sql);
            while (set.next()) {
                String c1 = set.getString(1);
                System.out.println("c1="+c1);
            }

        }catch (Exception e) {
            e.printStackTrace();
        }


    }
}
